<?php
namespace Admin\Controller;
class LogController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 获取开启记录
	 */
	public function getProjectLog(){
	    $order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
		
		}
		$currency_id=I('currency_id');
		$uid=I('uid');
		$username=I('username');
		$phone=I('userphone');
		$status=I('status');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['u.phone'] = $phone;
		}
		if(!empty($currency_id)){
			$where['o.currency_id'] = $currency_id;
		}
		if($status!=''){
			$where['o.status'] = $status;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('project')
		->alias('o')
		->field('o.*,u.username,u.phone,c.currency_mark')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		$count =M('project')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->count();
		//$list1=iconv('GBK','UTF-8',$list);
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	/**
	 * 获取动态奖励日志
	 */
	public function getLiziLog(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
		
		}
		$type=I('type');
		$currency_id=I('currency_id');
		$uid=I('uid');
		$username=I('username');
		$phone=I('userphone');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['u.phone'] = $phone;
		}
		if(!empty($type)){
			$where['o.type'] = $type;
		}
		if(!empty($currency_id)){
			$where['o.currency_id'] = $currency_id;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('lizi_log')
		->alias('o')
		->field('o.*,u.username,u.phone,c.currency_mark')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		$count =M('lizi_log')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->count();
		//$list1=iconv('GBK','UTF-8',$list);
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	public function getExchangeLog(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
	
		}
		//$currency_id=I('currency_id');
		$uid=I('uid');
		$username=I('username');
		$phone=I('userphone');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['u.phone'] = $phone;
		}
// 		if(!empty($type)){
// 			$where['o.type'] = $type;
// 		}
// 		if(!empty($currency_id)){
// 			$where['o.currency_id'] = $currency_id;
// 		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('exchange_log')
		->alias('o')
		->field('o.*,u.username,u.phone')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		//->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		$count =M('exchange_log')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		//->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->count();
		//$list1=iconv('GBK','UTF-8',$list);
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	/**
	 * 超级节点日志
	 */
	public function getPointLog(){
		$order = 'o.add_time desc';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
	
		}
		$uid=I('uid');
		$username=I('username');
// 		$currency_id=I('currency_id');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
// 		if(!empty($currency_id)){
// 			$where['o.currency_id'] = $currency_id;
// 		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('point_log')
		->alias('o')
		->field('o.*,u.username,u.phone,u.member_id')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		//dump($list);echo M('Coin_pay')->_sql();die;
		$count =M('point_log')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->where($where)
		->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	/**
	 * 充币日志
	 */
	public function getRechargeLog(){
		$order = 'o.add_time desc';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
		
		}
		$uid=I('uid');
		$username=I('username');
		$currency_id=I('currency_id');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
		if(!empty($currency_id)){
		    $where['o.currency_id'] = $currency_id;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('recharge')
		->alias('o')
		->field('o.*,u.username,u.phone,u.member_id,c.currency_id,c.currency_mark')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		//dump($list);echo M('Coin_pay')->_sql();die;
		$count =M('recharge')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	
	/**
	 * 提币日志
	 */
	public function getWithdrawLog(){
	    $order = 'o.add_time desc';
	    $sort = I('sort');
	    if(!empty($sort)){
	        if(!in_array($sort, [1,2])){
	            $data = [
	                'status'=>-1,
	                'msg'=>'参数错误，排序参数错误'
	            ];
	            $this->ajaxReturn($data);
	        }
	        $sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
	
	    }
	    $currency_id=I('currency_id');
	    $uid=I('uid');
	    $username=I('username');
	    $phone=I('userphone');
	    $status=I('status');
	    $begin = I('begin');
	    $last = I('last');
	    $page = I('page');
	    $num = I('num');
	    if(!empty($uid)){
	        $where['o.uid'] = $uid;
	    }
	    if(!empty($username)){
	        $where['u.username'] = array('like','%'.$username.'%');
	    }
	    if(!empty($phone)){
	        $where['u.phone'] = $phone;
	    }
	    if($status!=''){
	    	$where['o.status'] = $status;
	    }
	    if(!empty($currency_id)){
	    	$where['o.currency_id'] = $currency_id;
	    }
	    $whereTime = $this->dotime($begin, $last);
	    $where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
	    $pages = $this->DoPage($page, $num);
	    $list = M('withdraw')
	    ->alias('o')
	    ->field('o.*,u.username,u.phone,c.currency_id,c.currency_mark')
	    ->join('__MEMBER__ as u on u.member_id = o.uid')
	    ->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
	    ->where($where)
	    ->limit($pages['limit'],$pages['num'])
	    ->order($order)
	    ->select();
	    $count =M('withdraw')
	    ->alias('o')
	    ->join('__MEMBER__ as u on u.member_id = o.uid')
	    ->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
	    ->where($where)
	    ->count();
	    $return = [
	        'count'=>$count,
	        'data'=>$list,
	        'status'=>1
	    ];
	    $this->ajaxReturn($return);
	}
	//提币通过
	public function withdrawAdopt(){
	    $id = I('id');
	    $list = M('withdraw')->where(['id'=>$id])->find();
	    if(!$list||$list['status']!=0){
	        $return = [
	            'info'=>'参数错误',
	            'status'=>-1
	        ];
	        $this->ajaxReturn($return);
	    }
// 	    if($list['currency_id']!=2){
// 	        $currency = M('Coin')->where(['id'=>$list['type']])->find();
// 	        $this->qianbao_tibi($list['url'],$list['money'],$currency);
// 	    }
	    $r = M('withdraw')->where(['id'=>$id])->setField('status',1);
	    if($r){
	        $return = [
	            'info'=>'操作成功',
	            'status'=>1
	        ];
	        $this->ajaxReturn($return);
	    }else{
	        $return = [
	            'info'=>'系统繁忙',
	            'status'=>-1
	        ];
	        $this->ajaxReturn($return);
	    }
	}
	private function qianbao_tibi($url,$money,$currency){
	    require_once 'App/Common/Common/easybitcoin.php';
	    $bitcoin = new \Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_host'],$currency['rpc_port']);
	    //     	$result = $bitcoin->getinfo();
	    $bitcoin->walletlock();//制上锁
	    $bitcoin->walletpassphrase($currency['qianbao_key'],20);
	    $id=$bitcoin->sendtoaddress($url,$money);
	    $bitcoin->walletlock();
	    return $id;
	}
	//提币拒绝
	public function withdrawRefuse(){
	    $id = I('id');
	    $list = M('trade_log')->where(['id'=>$id])->find();
	    if(!$list||$list['status']!=0){
	        $return = [
	            'info'=>'参数错误',
	            'status'=>-1
	        ];
	        $this->ajaxReturn($return);
	    }
	    $r = M('trade_log')->where(['id'=>$id])->setField('status',-1);
// 	     if($list['type']==1){
// 	         $url = 'url1';
// 	     }else{
// 	         $url = 'url2';
// 	     }
	    M('Member_money')->where(['uid'=>$list['uid'],'currency_id'=>$list['currency_id']])->setInc('num',$list['num']);
	    $this->addFinance($list['uid'], 1, '提现拒绝返还', $list['num'], 1, $list['currency_id']);
	    if($r){
	        $return = [
	            'info'=>'操作成功',
	            'status'=>1
	        ];
	        $this->ajaxReturn($return);
	    }else{
	        $return = [
	            'info'=>'系统繁忙',
	            'status'=>-1
	        ];
	        $this->ajaxReturn($return);
	    }
	}
	/**
	 * 获取非遗链日志
	 */
	public function getFylLog(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
		
		}
		$type=I('type');
		$money_type=I('money_type');
		$currency_id=I('currency_id');
		$uid=I('uid');
		$username=I('username');
		$phone=I('userphone');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.member_id'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['u.phone'] = $phone;
		}
		if(!empty($type)){
			$where['o.type'] = $type;
		}
		if(!empty($money_type)){
			$where['o.money_type'] = $money_type;
		}
		if(!empty($currency_id)){
			$where['o.currency_id'] = $currency_id;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('finance_view')
		->alias('o')
		->field('o.*,u.username,u.phone,u.member_id,c.currency_mark,c.currency_id')
		->join('__MEMBER__ as u on u.member_id = o.member_id')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		$count =M('finance_view')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.member_id')
		->join('__CURRENCY__ as c on c.currency_id = o.currency_id')
		->where($where)
		->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	public function getCZlog(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'dotime':$order = 'dotime desc';
		
		}
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		$whereTime = $this->dotime($begin, $last);
		$where['dotime'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list=M('caozuolog')
			->where($where)
			->limit($pages['limit'],$pages['num'])
			->order($order)
			->select();
		$count=M('caozuolog')
			->where($where)
			->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	/**
	 * 充币日志
	 */
	public function getSweetLog(){
		$order = 'o.add_time desc';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
	
		}
		$uid=I('uid');
		$username=I('username');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.username'] = array('like','%'.$username.'%');
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('sweet_log')
		->alias('o')
		->field('o.*,u.username,u.phone,u.member_id')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		//dump($list);echo M('Coin_pay')->_sql();die;
		$count =M('sweet_log')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.uid')
		->where($where)
		->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
}



